<template>
  <div class="loginbox">
    <div class="login_top">
      登录
      <i class="login_cancel" @click="closeLogin">x</i>
    </div>
    <div class="login_body">
    <!-- <form name="form" method="post"> -->
    <!-- <form> -->
    <div class="left_body">
      <div class="user-box">
        <label for="username">账号：</label
        ><input
          id="username"
          type="text"
          maxlength="11"
          autocomplete="off"
          placeholder="手机号"
          name="username"
          ref="getUserValue"
        />
      </div>
      <div class="pwd-box">
        <img src="@/assets/images/closeEye.png" />
        <label for="pwd">密码：</label
        ><input id="pwd" type="password" placeholder="请输入密码" ref="getPwdValue" />
      </div>

      <div class="identity">
        <input
          type="radio"
          name="identity"
          value="用户"
          checked="checked"
        />用户
        <input
          type="radio"
          name="identity"
          value="管理员"
          style="margin-left: 20px"
        />管理员
      </div>
      <div class="operate">
        <button class="login" @click="login">登录</button>
        <!-- <input type="submit" name="login" value="登录" class="login" @click="login"/> -->
        <button class="register" @click="register">注册</button>
        <!-- <a href="register.html"
          ><input type="button" value="注册" class="register"
        /></a> -->
      </div>
    </div>
    <!-- </form> -->
    <div class="right_body">
      <a href="index.html">
        <h1><img src="@/assets/images/logo.png" />线上地摊</h1>
      </a>
    </div>
    </div>
  </div>
</template>

<script>
import { Toast } from 'vant'
import { mapState, mapMutations } from 'vuex'
export default {
  name: 'LoginPage',
  computed: {
    ...mapState(['userInfo'])
    // console.log(this.$store)
  },
  methods: {
    ...mapMutations(['changeLoginFlag', 'setNowUser', 'setUserInfo']),
    closeLogin () {
      document.querySelector('.loginbox').style.display = 'none'
    },
    // 判断账号密码是否已注册在localstorage
    isInStore (obj) {
      let i = 0
      let flag = false
      for (i = 0; i < this.userInfo.length; i++) {
        if (this.userInfo[i].username === obj.username && this.userInfo[i].pwd === obj.pwd) {
          flag = true
        }
      }
      return flag
    },
    // 登录按钮点击模块
    login () {
      this.setUserInfo(JSON.parse(localStorage.getItem('userInfo')))
      // const phoneTest = /(^1\d{10}$)|(^[0-9]\d{7}$)/
      // console.log(this.userInfo.length)
      const userInfomation =
        {
          username: this.$refs.getUserValue.value,
          pwd: this.$refs.getPwdValue.value
        }
      console.log(this.isInStore(userInfomation))
      // console.log(this.userInfo.includes(userInfomation))
      // console.log(this.userInfo.filter(userInfomation))
      // console.log(document.getElementById('pwd').value)
      // console.log(this.$refs.getPwdValue.value)
      if (this.$refs.getUserValue.value !== '' && this.$refs.getPwdValue.value !== '') {
        if (this.$refs.getUserValue.value.trim().length !== 11) {
          // if (!phoneTest.test(this.$refs.getUserValue.value)) {
          //   Toast({ message: '手机格式错误', position: top })
          //   return
          // }
          Toast({ message: '请输入正确的手机号码', position: top })
          return
        } else if (!this.isInStore(userInfomation)) {
          Toast({ message: '该手机号码未注册', position: top })
          return
        } else {
          Toast({ message: '登录成功', position: top })
        }
        console.log(userInfomation.username)
        this.setNowUser(userInfomation)
        // document.querySelector('.login_flag').innerText = JSON.parse(localStorage.getItem('userInfo')).username + ',欢迎您！'
        this.changeLoginFlag()
        document.querySelector('.loginbox').style.display = 'none'
      } else {
        Toast({ message: '用户名和密码不能为空', position: top })
      }
    },
    // 注册按钮点击模块
    register () {
      if (this.$refs.getUserValue.value !== '' && this.$refs.getPwdValue.value !== '') {
        const userInfo =
        [{
          username: this.$refs.getUserValue.value,
          pwd: this.$refs.getPwdValue.value
        }]
        localStorage.setItem('userInfo', JSON.stringify(userInfo))
      } else {
        Toast({ message: '用户名和密码不能为空', position: top })
        // alert('用户名和密码不能为空')
        return
      }
      Toast({ message: '注册成功', position: top })
    }
  }
}
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
}

.loginbox {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 530px;
  height: 360px;
  background-color: white;
  color: black;
  /* text-align: center; */
  z-index: 9998;
  border-radius: 4px;
  box-shadow: 0 5px 16px rgba(0, 0, 0, 0.8);
  border: none;
  display: none;
}
.login_top {
  position: relative;
  margin: 0;
  padding: 0 45px 0 18px;
  height: 38px;
  line-height: 38px;
  z-index: 10;
  border-bottom: 1px solid #191919;
  border-radius: 4px 4px 0 0;
  background: #2d2d2d;
  font-weight: bold;
  font-size: 14px;
  color: #fff;
}
.login_cancel {
  position: absolute;
  z-index: 20;
  top: 16px;
  right: 20px;
  width: 10px;
  height: 10px;
  color: white;
  overflow: hidden;
  text-indent: -9999px;
  cursor: pointer;
  background: url(https://p6.music.126.net/obj/wo3DlcOGw6DClTvDisK1/9660125932/c989/d009/7328/c3811556cb5c698eee7b2b7394413b1f.png)
    no-repeat;
  background-position: 0 -95px;
  text-align: left;
  font-size: inherit;
}
/* .show {
  display: block;
} */

.left_body {
  float: left;
  width: 320px;
  height: 320px;
  /* background-color: pink; */
  margin: 0px 15px;
  border-right: 1px solid rgb(255, 255, 255, 0.3);
}

/* .eye {
    background: url(@/assets/images/openEye.png) no-repeat fixed;
    background-size: 20px;
    background-position: 150px 15px;
} */

.user-box,.pwd-box {
  margin: 36px 0 0 34px;
  width: 240px;
  height: 30px;
  line-height: 30px;
  border: 1px solid #cccccc;
  border-radius: 15px;
  padding-left: 15px;
  font-size: 12px;
  box-shadow: none;
}

.user-box input,
.pwd-box input {
  padding-left: 5px;
  border: none;
  font-size: 12px;
  /* color: #fff; */
  background-color: transparent;
  outline: none;
}

.pwd-box img {
  float: right;
  width: 18px;
  margin: 5px 5px;
  cursor: pointer;
}

.operate {
  margin: 20px 0 0 58px;
}

.login {
  width: 60px;
  height: 40px;
  background: none;
  /* margin: 20px auto; */
  text-align: center;
  border: 2px solid #e6d6d6;
  border-radius: 25px;
  background-color: #e6d6d6;
  outline: none;
  color: black;
  cursor: pointer;
  margin: 10px;
}

.register {
  width: 60px;
  height: 40px;
  background: none;
  text-align: center;
  border-radius: 25px;
  outline: none;
  color: white;
  cursor: pointer;
  margin: 10px;
  background-color: #ff3a3a;
  border: 2px solid #ff3a3a;
  margin: 10px;
}

.ban {
  width: 60px;
  height: 40px;
  background: none;
  text-align: center;
  border-radius: 25px;
  outline: none;
  color: white;
  cursor: pointer;
  margin: 10px;
  background-color: #bccacb;
  border: 2px solid #bccacb;
  margin: 10px;
  cursor: not-allowed;
}

.identity {
  margin: 20px 0 0 82px;
  font-size: 14px;
}

.right_body {
  float: left;
  /* margin: 40px 0px 0 20px; */
  width: 168px;
  height: 300px;
  padding-top: 20px;
  border-left: 1px solid #757575;
  text-align: center;
  /* background-color: palegoldenrod; */
}

div img {
  width: 100px;
}

.right_body > a {
  text-decoration: none;
  color: black;
  font-size: 20px;
  line-height: 30px;
  text-align: justify;
  writing-mode: vertical-lr;
  font-family: "Franklin Gothic Medium", "Arial Narrow", Arial, sans-serif;
  /* writing-mode: lr-tb; */
}
</style>
